package com.tonghl.juc.CAS_study.code;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 资源类
 */
@Getter
@Setter
@AllArgsConstructor
class User{
    String userName;
    int    age;

}
/**
 * Created by tonghl
 *
 * CAS--->Unsafe--->CAS的底层思想---ABA问题--->原子引用更新--->如何规避ABA问题
 *
 * CAS中的ABA问题
 *  *    ABA：狸猫换太子
 *  *
 *  *    AtomicReference<User>/AtomicReference<Customer>
 */
public class AtomicReferenceDemo {

    public static void main(String[] args) {
        User z3=new User("z3",23);
        User li4=new User("li4",28);

        AtomicReference<User> atomicReference=new AtomicReference<>();
        atomicReference.set(z3);

        System.out.println(atomicReference.compareAndSet(z3, li4)+"\t"+atomicReference.get().userName);

        System.out.println(atomicReference.compareAndSet(z3, li4)+"\t"+atomicReference.get().userName);

    }
}
